package com.steakliu.sun.buffer;

import com.steakliu.sun.buffer.api.AbstractDataBuffer;
import com.steakliu.sun.buffer.api.DataBuffer;
import com.steakliu.sun.protocol.SunProtocol;
import com.steakliu.sun.spi.annotation.Join;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedTransferQueue;

/**
 * 功能说明：
 * <p>
 * Original @Author: steakliu-刘牌, 2023-02-08  22:22
 * <p>
 * Copyright (C)2020-2022  steakliu All rights reserved.
 */
@Join
public class QueueDataBuffer<T> extends AbstractDataBuffer<T> {

    private BlockingQueue<SunProtocol<T>> requestQueue;

    @Override
    public void push(SunProtocol<T> protocol) {
        try {
            requestQueue.put(protocol);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public SunProtocol<T> take() {
        try {
            return requestQueue.take();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void init(int capacity) {
        super.init(capacity);
        //TODO 请求缓存区大小 capacity
        this.requestQueue = new ArrayBlockingQueue<>(capacity);
    }
}
